package com.binhthuan.catechist.server;

import java.util.Properties;

import com.binhthuan.catechist.server.util.JsonConverter;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ErrorObject {

    public class Error {

        @Expose
        private int status;
        @Expose
        private Properties errors;
        @Expose
        @SerializedName(ErrorObject.REQUIRE_CONFIRMATION)
        private boolean requireConfirmation;

        public Error() {
            this(0);
        }

        public Error(int errorStatus) {
            status = errorStatus;
            errors = new Properties();
        }

        /**
         * @return the status
         */
        public int getStatus() {
            return status;
        }

        /**
         * @param status
         *            the status to set
         */
        public void setStatus(int status) {
            this.status = status;
        }

        /**
         * @return the requireConfirmation
         */
        public boolean getRequireConfirmation() {
            return requireConfirmation;
        }

        /**
         * @param requireConfirmation
         *            the requireConfirmation to set
         */
        public void setRequireConfirmation(boolean requireConfirmation) {
            this.requireConfirmation = requireConfirmation;
        }

        /**
         * @return the errors
         */
        public Properties getErrors() {
            return errors;
        }

        /**
         * @param errors
         *            the errors to set
         */
        public void setErrors(Properties errors) {
            this.errors = errors;
        }

        /**
         * Add an error.
         * 
         * @param source
         *            field caused error.
         * @param reason
         */
        public void addError(String source, String reason) {
            errors.put(source, reason);
        }
    }

    public static final String REQUIRE_CONFIRMATION = "_requireConfirmation";

    @Expose
    private Error response;

    public ErrorObject() {
        this(0);
    }

    public ErrorObject(int errorStatus) {
        response = new Error(errorStatus);
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return response.getStatus();
    }

    /**
     * @param status
     *            the status to set
     */
    public void setStatus(int status) {
        response.setStatus(status);
    }

    /**
     * @return the errors
     */
    public Properties getErrors() {
        return response.getErrors();
    }

    /**
     * @param errors
     *            the errors to set
     */
    public void setErrors(Properties errors) {
        response.setErrors(errors);
    }

    /**
     * @return the requireConfirmation
     */
    public boolean isRequireConfirmation() {
        return response.getRequireConfirmation();
    }

    /**
     * @param requireConfirmation
     *            the requireConfirmation to set
     */
    public void setRequireConfirmation(boolean requireConfirmation) {
        response.setRequireConfirmation(requireConfirmation);
    }

    /**
     * Add an error.
     * 
     * @param source
     *            field caused error.
     * @param reason
     */
    public void addError(String source, String reason) {
        getErrors().put(source, reason);
    }

    /**
     * @return the response
     */
    public Error getResponse() {
        return response;
    }

    /**
     * @param response
     *            the response to set
     */
    public void setResponse(Error response) {
        this.response = response;
    }

    public String toJson() {
        return JsonConverter.getInstance().toJson(this);
    }
}
